package deployr.core;

import java.io.*;
import java.nio.charset.*;
import java.util.zip.*;

public class Streams {

	public static final void write(final String content, final String path) {
		try {
			final PrintStream out = new PrintStream(new FileOutputStream(path));
			out.print(content);
			out.close();
		} catch (final Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static final OutputStream openOutputStreamOn(File file) {
		try {
			return new FileOutputStream(file);
		} catch (Exception e) {
			throw new RuntimeException("unable to open stream " + file.getAbsolutePath(), e);
		}
	}

	public static byte[] bytes(File file) {
		return bytes(inputStream(file));
	}

	public static byte[] bytes(InputStream inputStream) {
		try {
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			byte[] buf = new byte[1024];
			int len;
			while ((len = inputStream.read(buf)) > 0) {
				outputStream.write(buf, 0, len);
			}
			outputStream.flush();
			return outputStream.toByteArray();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static InputStream openInputStreamOn(File file) {
		try {
			return new FileInputStream(file);
		} catch (Exception e) {
			throw new RuntimeException("unable to open stream " + file.getAbsolutePath(), e);
		}
	}

	public static void write(byte[] bytes, OutputStream outputStream) {
		try {
			outputStream.write(bytes);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static final BufferedInputStream bufferedInputStream(String path) {
		return new BufferedInputStream(inputStream(file(path)));
	}

	public static final BufferedOutputStream bufferedOutputStream(File file) {
		return new BufferedOutputStream(outputStream(file));
	}

	public static final BufferedOutputStream bufferedOutputStream(String path) {
		return bufferedOutputStream(file(path));
	}

	public static final BufferedReader bufferedReader(File file) {
		return new BufferedReader(reader(dataInputStream(file)));
	}

	public static final BufferedReader bufferedReader(String path) {
		return new BufferedReader(reader(dataInputStream(file(path))));
	}

	public static final void close(InputStream inputStream) {
		try {
			inputStream.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static final void close(Reader reader) {
		try {
			reader.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void copy(byte[] bytes, File file) {
		try {
			OutputStream out = outputStream(file);
			out.write(bytes);
			out.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static final void copy(byte[] bytes, OutputStream outputStream) {
		try {
			copy(inputStream(bytes), outputStream);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void copy(byte[] bytes, String path) {
		File file = file(path);
		copy(bytes, file);
	}

	public static final void copy(File file, OutputStream outputStream) {
		try {
			copy(inputStream(file), outputStream);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void copy(InputStream inputStream, OutputStream outputStream) {
		try {
			byte[] buf = new byte[1024];
			int len;
			while ((len = inputStream.read(buf)) > 0) {
				outputStream.write(buf, 0, len);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static DataInputStream dataInputStream(File file) {
		return new DataInputStream(inputStream(file));
	}

	public static DataInputStream dataInputStream(InputStream inputStream) {
		return new DataInputStream(inputStream);
	}

	public static File file(File root, String path) {
		return new File(root, path);
	}

	public static File file(String path) {
		return new File(path);
	}

	public static File file(String root, String path) {
		return new File(root, path);
	}

	public static InputStream inputStream(byte[] bytes) {
		return new ByteArrayInputStream(bytes);
	}

	public static final FileInputStream inputStream(File file) {
		try {
			return new FileInputStream(file);
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	public static OutputStream outputStream(File file) {
		try {
			return new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	public static OutputStream outputStream(File file, boolean append) {
		try {
			return new FileOutputStream(file, append);
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	public static PrintStream printStream(File file) {
		return new PrintStream(outputStream(file));
	}

	public static PrintStream printStream(File file, boolean append) {
		return new PrintStream(outputStream(file, append));
	}

	public static PrintStream printStream(String path, boolean append) {
		return new PrintStream(outputStream(file(path), append));
	}

	public static InputStreamReader reader(DataInputStream in) {
		return new InputStreamReader(in);
	}

	public static InputStreamReader reader(InputStream in) {
		return new InputStreamReader(dataInputStream(in));
	}

	public static String slurp(InputStream stream) {
		try {
			StringBuffer out = new StringBuffer();
			byte[] buff = new byte[4096];
			for (int len; (len = stream.read(buff)) != -1;) {
				out.append(new String(buff, 0, len, Charset.forName("ISO-8859-1")));
			}
			return out.toString();

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static InputStreamReader stream(String path) {
		try {
			FileInputStream fis = new FileInputStream(new File(path));
			return new InputStreamReader(fis, "UTF-8");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static ZipInputStream zipInputStream(File file) {
		return new ZipInputStream(inputStream(file));
	}

	public static InputStream inputStream(StringBuilder stringBuilder) {
		return inputStream(stringBuilder.toString().getBytes());
	}
}
